﻿using System;
using System.Data;
using System.Data.SQLite;
using System.Globalization;
using System.Text;
using Stock.DBUtility;
using Stock.Model;

namespace Stock.Dal
{
    /// <summary>
    /// 数据访问类:GainHisDal
    /// </summary>
    public partial class GainHisDal
    {
        public GainHisDal()
        { }

        #region Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQLite.GetMaxID("ID", "StockOptHis");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from StockOptHis");
            strSql.Append(" where ID=@ID ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,8)};
            parameters[0].Value = ID;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(StockOptHisEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into StockOptHis(");
            strSql.Append("ID,UserID,StockID,Price,Copies,Amount,Type,Remark,OptDate)");
            strSql.Append(" values (");
            strSql.Append("@ID,@UserID,@StockID,@Price,@Copies,@Amount,@Type,@Remark,@OptDate)");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,8),
					new SQLiteParameter("@UserID", DbType.Int32,8),
					new SQLiteParameter("@StockID", DbType.String),
					new SQLiteParameter("@Price", DbType.Decimal,4),
					new SQLiteParameter("@Copies", DbType.Int32,8),
					new SQLiteParameter("@Amount", DbType.Decimal,4),
					new SQLiteParameter("@Type", DbType.String),
					new SQLiteParameter("@Remark", DbType.String),
					new SQLiteParameter("@OptDate", DbType.String)};
            parameters[0].Value = this.GetMaxId() + 1;
            parameters[1].Value = model.UserID;
            parameters[2].Value = model.StockID;
            parameters[3].Value = model.Price;
            parameters[4].Value = model.Copies;
            parameters[5].Value = model.Amount;
            parameters[6].Value = model.Type;
            parameters[7].Value = model.Remark;
            parameters[8].Value = model.OptDate;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(StockOptHisEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update StockOptHis set ");
            strSql.Append("UserID=@UserID,");
            strSql.Append("StockID=@StockID,");
            strSql.Append("Price=@Price,");
            strSql.Append("Copies=@Copies,");
            strSql.Append("Amount=@Amount,");
            strSql.Append("Type=@Type,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("OptDate=@OptDate");
            strSql.Append(" where ID=@ID ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@UserID", DbType.Int32,8),
					new SQLiteParameter("@StockID", DbType.String),
					new SQLiteParameter("@Price", DbType.Decimal,4),
					new SQLiteParameter("@Copies", DbType.Int32,8),
					new SQLiteParameter("@Amount", DbType.Decimal,4),
					new SQLiteParameter("@Type", DbType.String),
					new SQLiteParameter("@Remark", DbType.String),
					new SQLiteParameter("@OptDate", DbType.String),
					new SQLiteParameter("@ID", DbType.Int32,8)};
            parameters[0].Value = model.UserID;
            parameters[1].Value = model.StockID;
            parameters[2].Value = model.Price;
            parameters[3].Value = model.Copies;
            parameters[4].Value = model.Amount;
            parameters[5].Value = model.Type;
            parameters[6].Value = model.Remark;
            parameters[7].Value = model.OptDate;
            parameters[8].Value = model.ID;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from StockOptHis ");
            strSql.Append(" where ID=@ID ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,8)};
            parameters[0].Value = ID;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from StockOptHis ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public StockOptHisEntity GetModel(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,UserID,StockID,Price,Copies,Amount,Type,Remark,OptDate from StockOptHis ");
            strSql.Append(" where ID=@ID ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,8)};
            parameters[0].Value = ID;

            StockOptHisEntity model = new StockOptHisEntity();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserID"] != null && ds.Tables[0].Rows[0]["UserID"].ToString() != "")
                {
                    model.UserID = int.Parse(ds.Tables[0].Rows[0]["UserID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StockID"] != null && ds.Tables[0].Rows[0]["StockID"].ToString() != "")
                {
                    model.StockID = ds.Tables[0].Rows[0]["StockID"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Price"] != null && ds.Tables[0].Rows[0]["Price"].ToString() != "")
                {
                    model.Price = decimal.Parse(ds.Tables[0].Rows[0]["Price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Copies"] != null && ds.Tables[0].Rows[0]["Copies"].ToString() != "")
                {
                    model.Copies = int.Parse(ds.Tables[0].Rows[0]["Copies"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Amount"] != null && ds.Tables[0].Rows[0]["Amount"].ToString() != "")
                {
                    model.Amount = decimal.Parse(ds.Tables[0].Rows[0]["Amount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Type"] != null && ds.Tables[0].Rows[0]["Type"].ToString() != "")
                {
                    model.Type = ds.Tables[0].Rows[0]["Type"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Remark"] != null && ds.Tables[0].Rows[0]["Remark"].ToString() != "")
                {
                    model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                }
                if (ds.Tables[0].Rows[0]["OptDate"] != null && ds.Tables[0].Rows[0]["OptDate"].ToString() != "")
                {
                    model.OptDate = ds.Tables[0].Rows[0]["OptDate"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,UserID,StockID,Price,Copies,Amount,Type,Remark,OptDate ");
            strSql.Append(" FROM StockOptHis ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQLite.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@tblName", DbType.VarChar, 255),
                    new SQLiteParameter("@fldName", DbType.VarChar, 255),
                    new SQLiteParameter("@PageSize", DbType.Int32),
                    new SQLiteParameter("@PageIndex", DbType.Int32),
                    new SQLiteParameter("@IsReCount", DbType.bit),
                    new SQLiteParameter("@OrderType", DbType.bit),
                    new SQLiteParameter("@strWhere", DbType.VarChar,1000),
                    };
            parameters[0].Value = "StockOptHis";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion Method

        internal DateTime GetMaxDate()
        {
            DateTime backDt;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select max(CalcDate) as maxDate from GainHis");
            try
            {
                DataSet ds = DbHelperSQLite.Query(strSql.ToString());

                if (ds.Tables[0].Rows.Count > 0)
                {
                    DateTime.TryParseExact(ds.Tables[0].Rows[0]["maxDate"].ToString(), "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out backDt);
                }
                else
                {
                    DateTime.TryParseExact("1970-01-01", "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out backDt);
                }
            }
            catch
            {
                DateTime.TryParseExact("1970-01-01", "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out backDt);
            }
            return backDt;
        }

        internal int Add(GainHisEntity gainHisEntity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into GainHis(");
            strSql.Append("BankRate,Pre0,Pre1,Pre2,Pre3,CalcDate,BankAmount,Pre0Amount,Pre1Amount,Pre2Amount,Pre3Amount,FactSumAmount)");
            strSql.Append(" values (");
            strSql.Append("@BankRate,@Pre0,@Pre1,@Pre2,@Pre3,@CalcDate,@BankAmount,@Pre0Amount,@Pre1Amount,@Pre2Amount,@Pre3Amount,@FactSumAmount)");
            strSql.Append(";select LAST_INSERT_ROWID()");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@BankRate", DbType.Decimal,4),
					new SQLiteParameter("@Pre0", DbType.Decimal,4),
					new SQLiteParameter("@Pre1", DbType.Decimal,4),
					new SQLiteParameter("@Pre2", DbType.Decimal,4),
					new SQLiteParameter("@Pre3", DbType.Decimal,4),
					new SQLiteParameter("@CalcDate", DbType.String),
					new SQLiteParameter("@BankAmount", DbType.Decimal,4),
					new SQLiteParameter("@Pre0Amount", DbType.Decimal,4),
					new SQLiteParameter("@Pre1Amount", DbType.Decimal,4),
					new SQLiteParameter("@Pre2Amount", DbType.Decimal,4),
					new SQLiteParameter("@Pre3Amount", DbType.Decimal,4),
					new SQLiteParameter("@FactSumAmount", DbType.Decimal,4)};
            parameters[0].Value = gainHisEntity.BankRate;
            parameters[1].Value = gainHisEntity.Pre0;
            parameters[2].Value = gainHisEntity.Pre1;
            parameters[3].Value = gainHisEntity.Pre2;
            parameters[4].Value = gainHisEntity.Pre3;
            parameters[5].Value = gainHisEntity.CalcDate;
            parameters[6].Value = gainHisEntity.BankAmount;
            parameters[7].Value = gainHisEntity.Pre0Amount;
            parameters[8].Value = gainHisEntity.Pre1Amount;
            parameters[9].Value = gainHisEntity.Pre2Amount;
            parameters[10].Value = gainHisEntity.Pre3Amount;
            parameters[11].Value = gainHisEntity.FactSumAmount;

            object obj = DbHelperSQLite.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        internal DataSet GetDataList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,BankRate,Pre0,Pre1,Pre2,Pre3,CalcDate,BankAmount,Pre0Amount,Pre1Amount,Pre2Amount,Pre3Amount,FactSumAmount ");
            strSql.Append(" FROM GainHis order by CalcDate desc");

            return DbHelperSQLite.Query(strSql.ToString());
        }

        internal GainHisEntity GetModel(string p)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,BankRate,Pre0,Pre1,Pre2,Pre3,CalcDate,BankAmount,Pre0Amount,Pre1Amount,Pre2Amount,Pre3Amount,FactSumAmount from GainHis ");
            strSql.Append(" where CalcDate<=@CalcDate order by CalcDate desc");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@CalcDate", DbType.String)
};
            parameters[0].Value = p;

            GainHisEntity model = new GainHisEntity();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BankRate"] != null && ds.Tables[0].Rows[0]["BankRate"].ToString() != "")
                {
                    model.BankRate = decimal.Parse(ds.Tables[0].Rows[0]["BankRate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Pre0"] != null && ds.Tables[0].Rows[0]["Pre0"].ToString() != "")
                {
                    model.Pre0 = decimal.Parse(ds.Tables[0].Rows[0]["Pre0"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Pre1"] != null && ds.Tables[0].Rows[0]["Pre1"].ToString() != "")
                {
                    model.Pre1 = decimal.Parse(ds.Tables[0].Rows[0]["Pre1"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Pre2"] != null && ds.Tables[0].Rows[0]["Pre2"].ToString() != "")
                {
                    model.Pre2 = decimal.Parse(ds.Tables[0].Rows[0]["Pre2"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Pre3"] != null && ds.Tables[0].Rows[0]["Pre3"].ToString() != "")
                {
                    model.Pre3 = decimal.Parse(ds.Tables[0].Rows[0]["Pre3"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CalcDate"] != null && ds.Tables[0].Rows[0]["CalcDate"].ToString() != "")
                {
                    model.CalcDate = ds.Tables[0].Rows[0]["CalcDate"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BankAmount"] != null && ds.Tables[0].Rows[0]["BankAmount"].ToString() != "")
                {
                    model.BankAmount = decimal.Parse(ds.Tables[0].Rows[0]["BankAmount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Pre0Amount"] != null && ds.Tables[0].Rows[0]["Pre0Amount"].ToString() != "")
                {
                    model.Pre0Amount = decimal.Parse(ds.Tables[0].Rows[0]["Pre0Amount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Pre1Amount"] != null && ds.Tables[0].Rows[0]["Pre1Amount"].ToString() != "")
                {
                    model.Pre1Amount = decimal.Parse(ds.Tables[0].Rows[0]["Pre1Amount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Pre2Amount"] != null && ds.Tables[0].Rows[0]["Pre2Amount"].ToString() != "")
                {
                    model.Pre2Amount = decimal.Parse(ds.Tables[0].Rows[0]["Pre2Amount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Pre3Amount"] != null && ds.Tables[0].Rows[0]["Pre3Amount"].ToString() != "")
                {
                    model.Pre3Amount = decimal.Parse(ds.Tables[0].Rows[0]["Pre3Amount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FactSumAmount"] != null && ds.Tables[0].Rows[0]["FactSumAmount"].ToString() != "")
                {
                    model.FactSumAmount = decimal.Parse(ds.Tables[0].Rows[0]["FactSumAmount"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        internal DataSet GetDataGraphList(string p, string p_2)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CalcDate,BankAmount as 存款,FactSumAmount 实际金额,Pre1Amount 预期1 ");
            strSql.Append(" FROM GainHis ");

            strSql.Append(" where CalcDate >='" + p + "' and CalcDate <= '" + p_2 + "'");
            return DbHelperSQLite.Query(strSql.ToString());
        }
    }
}